package com.mall.orders.server.controller;

import com.mall.common.core.domain.Result;
import com.mall.common.core.web.controller.BaseController;
import com.mall.orders.common.domain.Chargeback;
import com.mall.orders.common.domain.Orders;
import com.mall.orders.common.domain.dto.OrdersDto;
import com.mall.orders.common.domain.vo.OrdersVo;
import com.mall.orders.server.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("order")
public class OrdersController extends BaseController {
    @Autowired
    private OrdersService ordersService;


    /**
     * 根据订单id查询订单
     * @param id
     * @return
     */
    @GetMapping("/findByOrdersId/{id}")
    public Result<OrdersVo> findById(@PathVariable("id") Long id){
        return Result.success(ordersService.selectOrdersById(id));
    }

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public Result list(OrdersDto ordersDto)
    {
        startPage();
        List<OrdersVo> list = ordersService.selectOrdersList(ordersDto);
        return getDataTable(list);
    }

    /**
     * 新增订单
     */
    @PostMapping
    public Result add(@RequestBody Orders orders)
    {
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改订单
     */
    @PutMapping
    public Result edit(@RequestBody Orders orders)
    {
        return toAjax(ordersService.updateOrders(orders));
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return toAjax(ordersService.deleteOrdersByIds(ids));
    }

    /**
     * 订单取消
     * @param orders
     * 要添加取消原因和备注
     */
    @PostMapping("/cancel")
    public Result cancel(@RequestBody Orders orders){
        ordersService.updateCancelStatus(orders);
        return Result.success();
    }


    /**
     * 修改收货地址
     */
    @PutMapping("/updateAddr")
    public Result updateAddr(@RequestBody OrdersVo orders){
        ordersService.updateAddr(orders);
        return Result.success();
    }

    /**
     * 修改总价
     */
    @PutMapping("/updateTotalMoney")
    public Result updateTotalMoney(@RequestBody OrdersVo orders){
        ordersService.updateTotalMoney(orders);
        return Result.success();
    }

    /**
     * 把订单状态改为已发货
     */
    @PutMapping("/updateOrderStatus/{id}")
    public Result updateOrderStatus(@PathVariable("id") Long id){
        ordersService.updateDeliverStatus(id);
        return Result.success();
    }
}
